package com.jackstraw.jack_base.datasource.database.generator.dao;

import com.jackstraw.jack_base.datasource.database.generator.dao.database.MySQLGeneratorDao;
import com.jackstraw.jack_base.datasource.database.generator.dao.database.OracleGeneratorDao;
import com.jackstraw.jack_base.datasource.database.generator.dao.database.PostgreSQLGeneratorDao;
import com.jackstraw.jack_base.datasource.database.generator.dao.database.SQLServerGeneratorDao;
import com.jackstraw.jack_base.datasource.database.parten.entity.bean.DataBaseRouting;
import com.jackstraw.jack_base.datasource.database.parten.exception.BadDataBaseException;
import com.jackstraw.jack_base.util.entity.enums.DataBaseEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;

/**
 * 服务类
 */
@Component
public class GeneratorDaoRouting {

    @Autowired
    private DataBaseRouting dataBaseRouting;

    @Autowired
    private MySQLGeneratorDao mySQLGeneratorDao;
    @Autowired
    private OracleGeneratorDao oracleGeneratorDao;
    @Autowired
    private SQLServerGeneratorDao sqlServerGeneratorDao;
    @Autowired
    private PostgreSQLGeneratorDao postgreSQLGeneratorDao;


    public GeneratorDao getGeneratorDao(DataBaseEnum dataBaseEnum) {
        switch (dataBaseEnum) {
            case MySQL:
                return mySQLGeneratorDao;
            case Oracle:
                return oracleGeneratorDao;
            case mssql:
                return sqlServerGeneratorDao;
            case PostgreSQL:
                return postgreSQLGeneratorDao;
        }
        return null;
    }


    /**
     * 获取连接的数据库schema
     *
     * @return
     */
    public DataBaseEnum getSchema() {
        Connection connection = null;
        try {
            connection = dataBaseRouting.getConnection();
            DatabaseMetaData dbmd = connection.getMetaData();
            String dataBaseType = dbmd.getDatabaseProductName();                  //获取数据库类型
            if (DataBaseEnum.mssql.getDatabaseProductName().equals(dataBaseType)) {
                return DataBaseEnum.mssql;
            } else if (DataBaseEnum.MySQL.getDatabaseProductName().equals(dataBaseType)) {
                return DataBaseEnum.MySQL;
            } else if (DataBaseEnum.Oracle.getDatabaseProductName().equals(dataBaseType)) {
                return DataBaseEnum.Oracle;
            } else if (DataBaseEnum.PostgreSQL.getDatabaseProductName().equals(dataBaseType)) {
                return DataBaseEnum.PostgreSQL;
            } else {
                throw new BadDataBaseException("002", "不支持当前数据库类型");
            }
        } catch (SQLException e) {
            throw new BadDataBaseException("001", "获取数据库类型异常", e);
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    throw new BadDataBaseException("003", "不支持当前数据库类型");
                }
            }
        }

    }
}
